 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager;

import java.util.Iterator;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.DiagramManager;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.diagram.IDiagramListener;
import com.vp.plugin.diagram.IDiagramUIModel;
import com.vp.plugin.model.IInstanceSpecification;
import com.vp.plugin.model.IInteractionLifeLine;
import com.vp.plugin.model.ILifeLine;
import com.vp.plugin.model.IModelElement;
import com.vp.plugin.model.IObject;
import com.vp.plugin.model.IObjectFlow;
import com.vp.plugin.model.factory.IModelElementFactory;

public class VPObjectDiagramManager {

	public static String PROP_CLASS_FILE_NAME = "PROP_CLASS_FILE_NAME";
	
	public IDiagramUIModel CreateSequenceDiagram(String diagramName) {
		
		  DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
		  ApplicationManager.instance().getViewManager().showMessage("Diag:"+DiagramManager.DIAGRAM_TYPE_OBJECT_DIAGRAM);
		  IDiagramUIModel diagModel = diagramManager.createDiagram(DiagramManager.DIAGRAM_TYPE_INTERACTION_DIAGRAM);		
		  diagModel.setName(diagramName);
		  diagModel.setAutoFitShapesSize(true);
		  diagramManager.openDiagram(diagModel);
		  return diagModel;
	}

	public IDiagramElement AddObjectToDiagram(IDiagramUIModel diagram, String objName) {
		
		  DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
		  IInstanceSpecification objectModelElement = IModelElementFactory.instance().createInstanceSpecification();
		  objectModelElement.setName(":"+objName);
		  
		  IDiagramElement diagramElement = diagramManager.createDiagramElement(diagram, objectModelElement);
		  diagramElement.getModelElement().setName(objName);
		  
		  return diagramElement;		
	}

	public IDiagramElement AddLifeLineToDiagram(IDiagramUIModel diagram, String objName, IModelElement classifier) {
		ApplicationManager.instance().getViewManager().showMessage("Diag1");		
		  DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
		  ApplicationManager.instance().getViewManager().showMessage("Diag2");
		  IInteractionLifeLine objectModelElement =  IModelElementFactory.instance().createInteractionLifeLine();
		  ApplicationManager.instance().getViewManager().showMessage("Diag3");
		  objectModelElement.setClass(true);

		  ApplicationManager.instance().getViewManager().showMessage("Diag3");
		  objectModelElement.setBaseClassifier(classifier.getName());
		  ApplicationManager.instance().getViewManager().showMessage("Diag4");
		  IDiagramElement diagramElement = diagramManager.createDiagramElement(diagram, objectModelElement);
		  ApplicationManager.instance().getViewManager().showMessage("Diag5");
		  SetLifeLineAligment(diagram, diagramElement );
		  ApplicationManager.instance().getViewManager().showMessage("Diag6");
		  return diagramElement;		
	}
	
	private void SetLifeLineAligment(IDiagramUIModel diagram, IDiagramElement diagramElement){

		Iterator iter = diagram.diagramElementIterator();
		
		
		int x = diagramElement.getX();

		while (iter.hasNext()) {
			IDiagramElement de = (IDiagramElement) iter.next();

			if ((de.getModelElement() instanceof IInteractionLifeLine)
					&& (de != diagramElement)) {
				int lastX = de.getX() + de.getWidth() + 10;
				x = (lastX > x) ? lastX : x; 
			} 
		}
		
		diagramElement.setY(diagramElement.getY() + 30);
		diagramElement.setX(x  + 40);
	}

	public void SetObjectPropery(IModelElement diagramElement, String propName, String propValue) {
		VPPropertyManager.instance().SetProperty(diagramElement, propName, propValue);
	}

	public String GetObjectPropery(IModelElement diagramElement, String propName) {
		return VPPropertyManager.instance().GetProperty(diagramElement, propName);
	}
}
